package imagetabpaine;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author rbs
 */
public class ImageTabPanel extends JPanel{

    ArrayList <Tab> tabs;
    private JPanel panel;
    private ImagePane imagePane;
    private int size;
    Dimension dim;
    private String border_orientation;
    private int imageSize;
    private int tab_border;

    private int horiTextPos = -1;
    private int vertTextPos = -1;
    private int startPanel;
    private JPanel borderPane;


    private Color tabCol;
    private Color backCol;
    private Color selCol;
    private int panelborder;
    private Boolean topborder = true;


    public ImageTabPanel(int size, String border)
    {
        this(new TabColors(Color.black, Color.gray, Color.white), size, border);
        backCol = null;
        selCol = null;
    }

    public ImageTabPanel(Color tabColor, Color backColor, Color selTabColor, int size, String border_orientation) {

        this.tabCol = tabColor;
        this.backCol = backColor;
        this.selCol = selTabColor;
        tabs = new ArrayList<Tab>();
        dim = new Dimension(600, 400);
        this.size = size;
        this.border_orientation = border_orientation;
        if (border_orientation == BorderLayout.NORTH || border_orientation == BorderLayout.SOUTH)
            this.imageSize = 80;
        else
            this.imageSize = 20;

        tab_border = 2;

        startPanel = 0;
        panel = new JPanel();

        panelborder = 15;
    }

    public ImageTabPanel(TabColors tabCols, int size, String border_orientation)
    {
        this.tabCol = tabCols.getTabColor();
        this.backCol = tabCols.getBackColor();
        this.selCol = tabCols.getSelTabColor();
        tabs = new ArrayList<Tab>();
        dim = new Dimension(600, 400);
        this.size = size;
        this.border_orientation = border_orientation;
        if (border_orientation.equals(BorderLayout.NORTH) || border_orientation.equals(BorderLayout.SOUTH))
            this.imageSize = 80;
        else
            this.imageSize = 20;

        tab_border = 2;

        startPanel = 0;
        panel = new JPanel();

        panelborder = 5;
    }

    public void setImageSize(int size)
    {
        this.imageSize = size;
    }

    public void setDimension(Dimension dim)
    {
        this.dim = dim;
    }

    public void setImagePanelHeight(int height)
    {
        this.size = height;
    }

    public void setTabBorder(int tab_border)
    {
        this.tab_border = tab_border;
    }

    public void setStartPanel(int startPanel)
    {
        this.startPanel = startPanel;
    }

    public void setTabColors(TabColors tabcol)
    {
        this.selCol = tabcol.getSelTabColor();
        this.backCol = tabcol.getBackColor();
        this.tabCol = tabcol.getTabColor();
    }

    public void AddTab(Tab tab)
    {
        Image img = tab.getIcon().getImage();
        
        if (border_orientation == BorderLayout.NORTH || border_orientation == BorderLayout.SOUTH)
        {
            int width = (int) (img.getWidth(this) / (img.getHeight(this) / (size * 0.01 * imageSize))) ;
            img = img.getScaledInstance(width, (int) (size * 0.01 * imageSize), Image.SCALE_DEFAULT);
        }
        else
        {
            int height = (int) (img.getHeight(this) / (img.getWidth(this) / (size * 0.01 * imageSize))) ;
            img = img.getScaledInstance((int) (size * 0.01 * imageSize) ,height,  Image.SCALE_DEFAULT);
        }
        tab.setIcon(new ImageIcon(img));
        tabs.add(tab);
    }

    public boolean RemoveTab(Tab tab)
    {
        if (tabs.contains(tab))
        {
            tabs.remove(tab);
        return true;
        }
        else
            return false;
    }

    public void setPanel(JPanel panel)
    {
        remove(this.panel);
        this.panel = panel;

        add(this.panel, BorderLayout.CENTER);
        validate();
        this.panel.repaint();
    }

    public void setTextPosition(int horizontal, int vertical)
    {
        this.horiTextPos = horizontal;
        this.vertTextPos = vertical;
    }

    public void finizialize()
    {
        this.setLayout(new BorderLayout());

        imagePane = new ImagePane(tabs, border_orientation, size, startPanel, backCol ,
                tabCol, selCol, tab_border, panelborder, topborder);
        if(horiTextPos != -1 && vertTextPos != -1)
            imagePane.setTextOrientation(horiTextPos, vertTextPos);

        imagePane.setTabBorder(tab_border);
        imagePane.setPanelBorder(panelborder);
        imagePane.setVisible(true);
        imagePane.addImagePaneEventListener(new ImagePaneEventListener() {

            public void onAction(ImagePaneEvent event) {
                setPanel(tabs.get(event.getTabNumber()).getPanel());
            }
        });

        setPanel(tabs.get(startPanel).getPanel());


        this.add(imagePane, border_orientation);

        this.add(panel, BorderLayout.CENTER);
        panel.setVisible(true);
        this.setVisible(true);
    }

    public void setPanelBorder(int border) {
        this.panelborder = border;
    }

    public void setTopBorder(Boolean border)
    {
        this.topborder = border;
    }

}

